home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / kaffe-0.2 / kaffe / exception.c < prev    next >
C/C++ Source or Header  |  1996-02-18  |  3KB  |  122 lines

  1. /*
  2.  * exception.c
  3.  * Handle exceptions.
  4.  *
  5.  * Copyright (c) 1996 Systems Architecture Research Centre,
  6.  *           City University, London, UK.
  7.  *
  8.  * See the file "license.terms" for information on usage and redistribution
  9.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  10.  *
  11.  * Written by Tim Wilkinson <tim@sarc.city.ac.uk>, February 1996.
  12.  */
  13.  
  14. #define    DEBUG
  15.  
  16. #include <stdio.h>
  17. #include <assert.h>
  18. #include <signal.h>
  19. #include "object.h"
  20. #include "classMethod.h"
  21. #include "exception.h"
  22. #include "baseClasses.h"
  23. #include "lookup.h"
  24. #include "md.h"
  25.  
  26. object*    ClassFormatError;
  27. object*    ClassFormatError;
  28. object*    LinkageError;
  29. object*    NoClassDefFoundError;
  30. object*    NoSuchFieldError;
  31. object*    NoSuchMethodError;
  32. object*    OutOfMemoryError;
  33. object*    UnsatisfiedLinkError;
  34. object*    VirtualMachineError;
  35. object*    ClassCircularityError;
  36. object*    NegativeArraySizeException;
  37. object*    ClassCastException;
  38. object*    IllegalMonitorStateException;
  39. object*    NullPointerException;
  40.  
  41. static void nullException(int);
  42.  
  43. /*
  44.  * Setup the internal exceptions.
  45.  */
  46. void
  47. initExceptions(void)
  48. {
  49. #define    EXCEPTION(s) alloc_object(lookupClass(addString(#s)), true)
  50.  
  51.     ClassFormatError = EXCEPTION(java/lang/ClassFormatError);
  52.     LinkageError = EXCEPTION(java/lang/LinkageError);
  53.     NoClassDefFoundError = EXCEPTION(java/lang/NoClassDefFoundError);
  54.     NoSuchFieldError = EXCEPTION(java/lang/NoSuchFieldError);
  55.     NoSuchMethodError = EXCEPTION(java/lang/NoSuchMethodError);
  56.     OutOfMemoryError = EXCEPTION(java/lang/OutOfMemoryError);
  57.     UnsatisfiedLinkError = EXCEPTION(java/lang/UnsatisfiedLinkError);
  58.     VirtualMachineError = EXCEPTION(java/lang/VirtualMachineError);
  59.     ClassCircularityError = EXCEPTION(java/lang/ClassCircularityError);
  60.     NegativeArraySizeException = EXCEPTION(java/lang/NegativeArraySizeException);
  61.     ClassCastException = EXCEPTION(java/lang/ClassCastException);
  62.     IllegalMonitorStateException = EXCEPTION(java/lang/IllegalMonitorStateException);
  63.     NullPointerException = EXCEPTION(java/lang/NullPointerException);
  64.  
  65.     initClasses();
  66.  
  67. #if !defined(DEBUG)
  68.     /* Catch signal we need to convert to exceptions */
  69. #if defined(SIGSEGV)
  70.     signal(SIGSEGV, nullException);
  71. #endif
  72. #if defined(SIGBUS)
  73.     signal(SIGBUS, nullException);
  74. #endif
  75. #endif
  76. }
  77.  
  78. /*
  79.  * Throw an exception.
  80.  */
  81. void
  82. throwException(object* eobj, void* fptr)
  83. {
  84.     exceptionFrame* firstFrame;
  85.     exceptionFrame* frame;
  86.     classes* class;
  87.     exceptionInfo einfo;
  88.  
  89.     if (eobj == 0) {
  90.         fprintf(stderr, "Exception thrown on null object ... aborting\n");
  91.         exit(1);
  92.     }
  93.  
  94.     class = eobj->mtable->class;
  95.  
  96.     firstFrame = FIRSTFRAME(fptr);
  97.     for (frame = firstFrame; frame != 0; frame = NEXTFRAME(frame)) {
  98.         findExceptionInMethod((nativecode*)frame->retpc, class, &einfo);
  99.         if (einfo.handler == 0) {
  100.             continue;
  101.         }
  102.         /* Goto the exception handler */
  103.         CALL_KAFFE_EXCEPTION(frame, einfo, eobj);
  104.     }
  105.  
  106.     fprintf(stderr, "Failed to catch exception ... aborting:\n\t%s\n", class->name);
  107.     fflush(stderr);
  108.     exit(1);
  109. }
  110.  
  111. /*
  112.  * Null exception - catches bad memory accesses.
  113.  */
  114. static
  115. void
  116. nullException(int sig)
  117. {
  118.     /* I really need to check the fault address is null but I dont
  119.        have the info at the moment. */
  120.     throwException(NullPointerException, 0 /* dummy */);
  121. }
  122.